//
//  HDCFWisdomRiskTestVc.m
//  HaoDeCaiFu
//
//  Created by 郑开宏 on 2017/11/9.
//  Copyright © 2017年 郑开宏. All rights reserved.
//

#import "HDCFWisdomRiskTestVc.h"
#import "wisdomQuestionView.h"
#define KUSERASSESS @"api/User/Assess"
#import "SVProgressHUD.h"
#import "HDCFWisdomTestResultViewController.h"

@interface HDCFWisdomRiskTestVc ()<wisdomQuestionViewDelegate, UIScrollViewDelegate>

@property (nonatomic, strong) NSArray *titleArray;

@property (nonatomic, strong) NSArray *optionAryOne;
@property (nonatomic, strong) NSArray *optionAryTwo;
@property (nonatomic, strong) NSArray *optionAryThree;
@property (nonatomic, strong) NSArray *optionAryFour;
@property (nonatomic, strong) NSArray *optionAryFive;
@property (nonatomic, strong) NSArray *imageArray;
@property (nonatomic, strong) NSMutableDictionary *chooseDic;

@property (nonatomic, strong) NSMutableArray *questionviewArray;
@property (nonatomic, weak) UIScrollView *scrollView;

@end

@implementation HDCFWisdomRiskTestVc

-(NSMutableDictionary*)chooseDic
{
    if (!_chooseDic) {
        _chooseDic=[NSMutableDictionary dictionary];
    }
    return _chooseDic;
}

- (NSArray *)imageArray{
    if (!_imageArray) {
        self.imageArray = [NSArray arrayWithObjects:@"bg1", @"bg2", @"bg3", @"bg4", @"bg5", nil];
    }
    return _imageArray;
}

- (NSMutableArray *)questionviewArray{
    if (!_questionviewArray) {
        self.questionviewArray = [NSMutableArray array];
    }
    return _questionviewArray;
}

- (NSArray *)titleArray{
    if (!_titleArray) {
        self.titleArray = [NSArray arrayWithObjects:@"您的可投资资金大約为：", @"您面临一项可能的投资损失，以下哪种情况是您最容易接受的：", @"您持有的一项投资在过去三年第一年赚11%，第二年赚10%，第三年赚12%，您更倾向于：", @"您认为自己更偏向是一个：", @"当您的总投资资产浮亏多少时您非常焦虑：", nil];
    }
    return _titleArray;
}

- (NSArray *)optionAryOne{
    if (!_optionAryOne) {
        self.optionAryOne = [NSArray arrayWithObjects:@"10万元以下", @"10万-50万", @"50万-100万 ", @"100万以上", nil];
    }
    return _optionAryOne;
}

- (NSArray *)optionAryTwo{
    if (!_optionAryTwo) {
        self.optionAryTwo = [NSArray arrayWithObjects:@"肯定会损失600元", @"20%的机会损失6600元", @"1%的机会损失88000元",nil];
    }
    return _optionAryTwo;
}

- (NSArray *)optionAryThree{
    if (!_optionAryThree) {
        self.optionAryThree = [NSArray arrayWithObjects:@"加仓买入", @"减仓卖出", nil];
    }
    return _optionAryThree;
}

- (NSArray *)optionAryFour{
    if (!_optionAryFour) {
        self.optionAryFour = [NSArray arrayWithObjects:@"长期投资者，寻求资产的长期价值升值来获利", @"短期交易者，利用资产短期波动频繁交易获利", nil];
    }
    return _optionAryFour;
}

- (NSArray *)optionAryFive{
    if (!_optionAryFive) {
        self.optionAryFive = [NSArray arrayWithObjects:@"即使是非常细小的亏损也会很焦虑", @"亏损超过10%", @"亏损超过20%", @"亏损超过30%", nil];
    }
    return _optionAryFive;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationItem setTitle:@"智能投顾(共5题)"];
    UIScrollView *bottomScorll = [[UIScrollView alloc] init];
    self.scrollView = bottomScorll;
    bottomScorll.delegate = self;
    bottomScorll.bounces = NO;
    bottomScorll.pagingEnabled = YES;
    bottomScorll.frame = CGRectMake(0, 0, screenWidth, screenHeight - KNavHeight);
    bottomScorll.showsVerticalScrollIndicator = NO;
    bottomScorll.showsHorizontalScrollIndicator = NO;
    bottomScorll.backgroundColor = [UIColor whiteColor];
    for (int i = 0; i < 5; i++) {
        UIView *view = [[UIView alloc] init];
        view.tag=i;
        view.frame = CGRectMake(i * screenWidth, 0, screenWidth, screenHeight - KNavHeight);
        wisdomQuestionView *quesView = [wisdomQuestionView wisdomQuestionView];
        quesView.viewTag = i;
        quesView.tag=222;
        NSString *tile = self.titleArray[i];
        quesView.titleString = tile;
        quesView.delegate = self;
        NSString *imgName = self.imageArray[i];
        quesView.imgName = imgName;
        [self.questionviewArray addObject:quesView];
        if (i == 0) {
            quesView.optionArray = self.optionAryOne;
        }else if (i == 1){
            quesView.optionArray = self.optionAryTwo;
        }else if (i == 2){
            quesView.optionArray = self.optionAryThree;
        }else if (i == 3){
            quesView.optionArray = self.optionAryFour;
        }else if (i == 4){
            quesView.optionArray = self.optionAryFive;
        }
        quesView.frame = CGRectMake(20, 30, screenWidth - 40, (screenWidth - 40) * 851.0 / 612);
        [view addSubview:quesView];
        [bottomScorll addSubview:view];
    }
    [bottomScorll setContentSize:CGSizeMake(screenWidth, screenHeight - KNavHeight)];
    [self.view addSubview:bottomScorll];
}

#pragma mark 代理方法
- (void)returnSlectOption:(NSInteger)index WithViewTag:(NSInteger)tag{
    if (tag < 4) {
        [self.scrollView setContentSize:CGSizeMake(screenWidth * (tag + 2), screenHeight - KNavHeight)];
        
        LSWeakSelf;
        [UIView animateWithDuration:0.5 animations:^{
            weakSelf.scrollView.contentOffset=CGPointMake(screenWidth*(tag+1), 0);
        }];
        
    
    }
    LSWeakSelf;
    NSString *tagKeyStr=[NSString stringWithFormat:@"%lu",tag+1];
    NSString *indexValueStr=[self numberToABCWith:index+1];
    [self.chooseDic setObject:indexValueStr forKey:tagKeyStr];
    if (tag==4) {
       NSString *jsonStr=[self dictionaryToJson:self.chooseDic];
        [HttpRequest sendSelfPostRequestWithURL:KUSERASSESS WithParamets:jsonStr WithParamet:nil success:^(id result) {
            NSDictionary *resultDic=(NSDictionary*)result;
            NSNumber *number=[result objectForKey:@"code"];
            if ([number integerValue]==1) {
                NSDictionary*dic=[resultDic objectForKey:@"result"];
                NSString*type=[dic objectForKey:@"type"];
                NSString*portfolioCode=[dic objectForKey:@"portfolioCode"];
                NSLog(@"%@",type);
                HDCFWisdomTestResultViewController*vc=[[HDCFWisdomTestResultViewController alloc]init];
                vc.style=type;
                vc.portfolioCode=portfolioCode;
                vc.NewTestButtonActionBlock = ^{
                    
                    for (NSInteger i=0; i<5; i++) {
                        UIView*view=[weakSelf.scrollView viewWithTag:i];
                        wisdomQuestionView*questionView=[view viewWithTag:222];
                        questionView.selectNO=-1;
                        questionView.isSelect=NO;
                        [questionView.tableView reloadData];
                    }
                weakSelf.scrollView.contentOffset=CGPointMake(0, 0);
                };
                [self.navigationController pushViewController:vc animated:YES];
            }
            else
            {
                [SVProgressHUD showInfoWithStatus:[resultDic objectForKey:@"msg"]];
                [SVProgressHUD dismissWithDelay:1.0];
            }
            
            
        } failure:^(NSError *errer) {
            [SVProgressHUD showErrorWithStatus:@"网络错误"];
            [SVProgressHUD dismissWithDelay:1.0];
        }];
    }
    
//
}

- (NSString *)dictionaryToJson:(NSDictionary *)dic{
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error];
   NSString *jsonString=[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSString *jsonTemp = [jsonString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    return jsonTemp;
}

-(NSString*)numberToABCWith:(NSInteger)index
{
    NSString *str=@"";
    if (index==1) {
        str=@"a";
    }
    else if (index==2)
    {
        str=@"b";
    }
    else if (index==3)
    {
        str=@"c";
    }
    else if (index==4)
    {
        str=@"d";
    }
    return str;
}


@end
